﻿using System;
using System.Collections.Generic;
using EntityFrameworkExtension;
using EntityFrameworkExtension.Caching;
using EntityFrameworkExtension.Interface;
using NUnit.Framework;
using Rhino.Mocks;

namespace Test.Main.Caching
{
    [TestFixture]
    public class DataCacheTests 
    {
        private IDataCache cache;
        private IEfContext efContext;

        private readonly List<ReferenceTypeTable1> referenceTypeTable1Mappings = new List<ReferenceTypeTable1>
                                                                            {
                                                                                new ReferenceTypeTable1
                                                                                    {Id = 2, Name = "DEFAULT_NAME"}
                                                                            };

        [SetUp]
        public void SetUp()
        {
            efContext = MockRepository.GenerateMock<IEfContext>();
            cache = new DataCache();
        }

        [TearDown]
        public void Teardown()
        {
            efContext.VerifyAllExpectations();
        }

        [Test]
        public void FirstCallToGetTypeMappingCallsEfContext()
        {
            efContext.Expect(x => x.GetObjectList<ReferenceTypeTable1>()).Return(referenceTypeTable1Mappings);

            var results = cache.GetTypeMapping<ReferenceTypeTable1>(efContext);

            Assert.AreEqual(referenceTypeTable1Mappings, results);
        }

        [Test]
        public void SecondCallToGetTypeMappingShouldUseCachedData()
        {
            efContext.Expect(x => x.GetObjectList<ReferenceTypeTable1>()).Return(referenceTypeTable1Mappings).Repeat.Once();

            var results1 = cache.GetTypeMapping<ReferenceTypeTable1>(efContext);
            var results2 = cache.GetTypeMapping<ReferenceTypeTable1>(efContext);

            Assert.AreEqual(referenceTypeTable1Mappings, results1);
            Assert.AreEqual(referenceTypeTable1Mappings, results2);
        }


    }
}
